# pip install kafka-python
from kafka import KafkaProducer
import time
# 1、创建生产者
producer = KafkaProducer(bootstrap_servers="master:9092")

# 2、读取文件
with open("../data/cars.json", mode="r", encoding="utf-8") as f:
    lines = [line.strip() for line in f.readlines()]

# kafka-topics.sh --bootstrap-server master:9092 --create --topic students_partition --partitions 3
# 3、循环将数据写入kafka
for line in lines:
    producer.send(topic="cars", value=line.encode("utf-8"))
    producer.flush()

    time.sleep(0.01)

producer.close()
